﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TLCApp.pulic;
using TLCApp.StkForm;

namespace TLCApp
{
    public partial class stk_scck : Form
    {

        public stk_scck(string pappno)
        {
            InitializeComponent();
            mappno = pappno;
        }
        private string mappno = "";
        private string _stype = "0"; //0新增 1修改, 2查看;  
       private BindingSource bsmx = new BindingSource();
   
        /// <summary>
        /// 传入值
        /// </summary>
        public string stype
        {
            get { return _stype; }
            set
            {
                if (value == null)
                {
                    return;
                }
                _stype = value;
            }

        }
        private string _rklx = "0";//出库类型：0 半成品出库：1配件出库 ，2发货出库
        public string rklx
        {
            get { return _rklx; }
            set
            {
                if (value == null)
                {
                    return;
                }
                _rklx = value;
            }

        }
        private string _kflx = "0";//-----0成品库, 1半成品 2,配件,3 材料
        public string kflx
        {
            get { return _kflx; }
            set
            {
                if (value == null)
                {
                    return;
                }
                _kflx = value;
            }

        }
        private bool _endable =true ;// 
        public bool mendable
        {
            get { return _endable; }
            set
            {
              
                _endable = value;
            }

        }
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtBMMC_KeyDown(object sender, KeyEventArgs e)
        {
           
            if (e.KeyCode == Keys.Enter)
            {      
                DataGridViewRow mSelectRow = new DataGridViewRow();
                DataTable dt = new DataTable();
                if (txthtbh.Text.Trim().Length==0)
                {
                    dt= Dal.stk.stk_mb_query("1").Tables[0];
                    Frm_Query frmshow = new Frm_Query(dt, "");
                    frmshow.filter = "代码|名称|拼音|五笔|规格|宽1|宽2|宽3|高";
                    frmshow.strwhere = txtBMMC.Text.Trim();
                    frmshow.ShowDialog();
                    mSelectRow = frmshow.mSelectRow;
                    if (mSelectRow != null && mSelectRow.Index >= 0)
                    {
                        txtBMDM.Text = mSelectRow.Cells["代码"].Value.ToString();
                        this.txtBMMC.Text = mSelectRow.Cells["名称"].Value.ToString();
                        txtgg.Text = mSelectRow.Cells["规格"].Value.ToString();
                        this.txtdw.Text = mSelectRow.Cells["单位"].Value.ToString();
                        if (kflx == "1")
                        {
                            cmbtype.SelectedValue = "2";
                        }
                        else
                        {
                            cmbtype.SelectedValue = (mSelectRow.Cells["是否新"].Value.ToString().ToUpper() == "Y") ? "1" : "2";
                        }

                    }
                    else
                    {

                        return;
                    }
                }
                else
                {
                    dt = Dal.stk.stk_mb_query("0").Tables[0];
                    Frm_Query frmshow = new Frm_Query(dt, txthtbh.Text.Trim());
                    frmshow.filter = "合同编号|代码|名称|拼音|五笔|规格|宽1|宽2|宽3|高";
                    frmshow.strwhere = txtBMMC.Text.Trim();
                    frmshow.ShowDialog();
                    mSelectRow = frmshow.mSelectRow;
                    if (mSelectRow != null && mSelectRow.Index >= 0)
                    {
                        txtBMDM.Text = mSelectRow.Cells["代码"].Value.ToString();
                        this.txtBMMC.Text = mSelectRow.Cells["名称"].Value.ToString();
                        txtgg.Text = mSelectRow.Cells["规格"].Value.ToString();
                        this.txtdw.Text = mSelectRow.Cells["单位"].Value.ToString();
                        if (kflx == "1")
                        {
                            cmbtype.SelectedValue = "2";
                        }
                        else
                        {
                            cmbtype.SelectedValue = (mSelectRow.Cells["是否新"].Value.ToString().ToUpper() == "Y") ? "1" : "2";
                        }

                    }
                    else
                    {

                        return;
                    }
                }

                txtsl.Focus();
              
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.bsmx.AddNew();
            txtBMMC.Focus();
        }

        private void stk_scrk_Load(object sender, EventArgs e)
        {
             setcontrol();
             cmbkf.DataSource = Dal.stk.Sys_GetStock(kflx).Tables[0];
             cmbkf.DisplayMember = "stockmc";
             cmbkf.ValueMember = "stockid";
             this.cmbks.DataSource = Dal.stk.Sys_GetKs("2").Tables[0];
             cmbks.DisplayMember = "cmn_dept_mc";
             cmbks.ValueMember = "cmn_deptcode";
             cmbtype.DataSource = Cls_Func.GetBaseData("S01");
             cmbtype.DisplayMember = "DataName";
             cmbtype.ValueMember = "DataType";
            querydata(mappno);
            bingdata();
            txthtbh.Focus();

        }
        void bingdata()
        {
            try
            {
                this.txtBMDM.DataBindings.Add("Text", bsmx, "xmdm");
                txtBMMC.DataBindings.Add("Text", bsmx, "xmmc");
                this.txtgg.DataBindings.Add("Text", bsmx, "matermodel");
                cmbtype.DataBindings.Add("Text", bsmx, "xmlbmc");
                cmbtype.DataBindings.Add("SelectedValue", bsmx, "xmlb");   
                this.txtsl.DataBindings.Add("Text", bsmx, "sl");
                txtbz.DataBindings.Add("Text", bsmx, "BZ");
                txtdw .DataBindings.Add("Text", bsmx, "unitname");
            }
            catch(Exception ex )
            {

                MessageBox.Show(ex.ToString());
            }
         
              
                
        }
        DataSet ds = new DataSet();
        void querydata( string appno)
        {
            try
            {
                
                    setbtn(mendable);

                    ds = Dal.stk.STK_BCP_CKD_data(appno);
                ds.Tables[0].TableName = "STK_CKD";
                ds.Tables[1].TableName = "STK_CKDMX";
                DataTable dtzb = ds.Tables[0];
                bsmx.DataSource = ds.Tables[1];
                dgvmx.DataSource = bsmx;
                if (appno == "")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["CKLX"] = rklx;
                    dr["lrry"] = pulic.UserIfo.userId;
                    dr["lrrymc"] = pulic.UserIfo.userName;
                    dr["ckrq"] = DateTime.Now.ToString("yyyy-MM-dd");
                    ds.Tables[0].Rows.Add(dr);
                }
                txtrkd.DataBindings.Add("Text", ds.Tables[0], "appno");
                txthtbh.DataBindings.Add("Text", ds.Tables[0], "htbh");
                cmbkf.DataBindings.Add("SelectedValue", ds.Tables[0], "stockid");
                cmbks.DataBindings.Add("SelectedValue", ds.Tables[0], "ksdm");
                dtprkrq.DataBindings.Add("Value", ds.Tables[0], "ckrq");
                txtrkry.DataBindings.Add("Text", ds.Tables[0], "lrrymc");
                txtrkry.DataBindings.Add("Tag", ds.Tables[0], "lrry");
                //txtysry.DataBindings.Add("Text", ds.Tables[0], "ysrymc");
                //txtysry.DataBindings.Add("Tag", ds.Tables[0], "ysry");

                //txtbgry.DataBindings.Add("Text", ds.Tables[0], "bgymc");
                //txtbgry.DataBindings.Add("Tag", ds.Tables[0], "bgy");
                txthtbz.DataBindings.Add("Text", ds.Tables[0], "bz");

                //txtrkd.Text = dtzb.Rows[0]["appno"].ToString();
                //txthtbh.Text = dtzb.Rows[0]["htbh"].ToString();
                //cmbkf.SelectedValue = dtzb.Rows[0]["stockid"].ToString();
                //cmbks.SelectedValue = dtzb.Rows[0]["ksdm"].ToString();
                //if (dtzb.Rows[0]["rkrq"] != null)
                //{
                //    dtprkrq.Value = Convert.ToDateTime(dtzb.Rows[0]["rkrq"]);
                //}
                //txtrkry.Text = dtzb.Rows[0]["lrrymc"].ToString();
                //txtrkry.Tag = dtzb.Rows[0]["lrry"].ToString();
                //txtysry.Text = dtzb.Rows[0]["ysrymc"].ToString();
                //txtysry.Tag = dtzb.Rows[0]["ysry"].ToString();
                //txtbgry.Text = dtzb.Rows[0]["bgymc"].ToString();
                //txtbgry.Tag = dtzb.Rows[0]["bgy"].ToString();
                //txthtbz.Text = dtzb.Rows[0]["bz"].ToString();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
         
        }
        void setcontrol()
        {
            dgvmx.AutoGenerateColumns = false;
            this.dgvmx.AllowUserToAddRows = false;
            this.dgvmx.MultiSelect = false;
            this.dgvmx.ReadOnly = true;
            this.dgvmx.RowHeadersWidth = 10;
            this.dgvmx.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        }

        private void dgvmx_Click(object sender, EventArgs e)
        {
            if (dgvmx.RowCount > 0)
            {

               
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (0 == this.bsmx.Count)
                return;
            if (!TLCApp.pulic.Cls_Popup.MsgYesNoBox("请确认是否修改！"))
            {
                return;
            }
            
            DataRowView dv = (DataRowView)bsmx.Current;

            dv.Delete();
          
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        void clearband()
        {

            txtrkd.DataBindings.Clear();
            txthtbh.DataBindings.Clear();
            cmbkf.DataBindings.Clear();
            cmbks.DataBindings.Clear();
            dtprkrq.DataBindings.Clear();
            txtrkry.DataBindings.Clear();
            txtrkry.DataBindings.Clear();
            txtysry.DataBindings.Clear();
            txtysry.DataBindings.Clear();

            txtbgry.DataBindings.Clear();
            txtbgry.DataBindings.Clear();
            txthtbz.DataBindings.Clear();

            ////////////////明细
            this.txtBMDM.DataBindings.Clear();
            txtBMMC.DataBindings.Clear();
            this.txtgg.DataBindings.Clear();
            cmbtype.DataBindings.Clear();
            cmbtype.DataBindings.Clear();
            this.txtsl.DataBindings.Clear();
            txtbz.DataBindings.Clear();
            txtdw.DataBindings.Clear();
        }
        private void button3_Click(object sender, EventArgs e)
        {
           
                 try
                     {
                          

                         string strXml = TLCApp.pulic.XmlDataService.ToString(ds);
                         DataSet dsResult = Dal.stk.STK_BCP_CKD_save(strXml);

                         if (dsResult.Tables[0].Rows[0]["rst"].ToString().ToUpper() == "T")
                         {
                             TLCApp.pulic.Cls_Popup.MsgBox("保存成功！");

                             clearband();
                             querydata(dsResult.Tables[0].Rows[0]["appno"].ToString());
                             bingdata();
                         }
                         else
                         {
                             TLCApp.pulic.Cls_Popup.MsgBox(dsResult.Tables[0].Rows[0]["msg"].ToString());
                         }
                     }
                     catch (Exception ex)
                     {

                         TLCApp.pulic.Cls_Popup.MsgErrBox(ex.ToString());
                     }
        }

        private void txtsl_Leave(object sender, EventArgs e)
        {
            if (txtBMDM.Text.Length > 0)
            {
                if (pulic.Cls_Func.CheckString(txtsl.Text.Trim()) != 5)
                {
                    TLCApp.pulic.Cls_Popup.MsgErrBox("数量必须输入正整数");
                    txtsl.Focus();
                }
           
            }
        }

        private void txthtbh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DataGridViewRow mSelectRow = new DataGridViewRow();
                DataTable dt = new DataTable();
                dt = Dal.stk.Sys_Gethtxx().Tables[0];
                Frm_Query frmshow = new Frm_Query(dt, "");
                frmshow.filter = "合同编号|客户|拼音|五笔|项目名称|项目地址|联系人";
                frmshow.strwhere = txthtbh.Text.Trim();
                frmshow.chKht.Visible = false;
                frmshow.chKht.Checked = false;
                frmshow.ShowDialog();
                mSelectRow = frmshow.mSelectRow;
                if (mSelectRow != null && mSelectRow.Index >=0)
                {
                    this.txthtbh.Text = mSelectRow.Cells["合同编号"].Value.ToString();

                    cmbkf.Focus();
                }
                else
                {
                    
                    return;
                }
            }
        }

        private void txtbgry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DataGridViewRow mSelectRow = new DataGridViewRow();
                DataTable dt = new DataTable();
                dt = Dal.stk.Sys_GetRYXX().Tables[0];
                Frm_Query frmshow = new Frm_Query(dt, "");
                frmshow.filter = "代码|名称|拼音|五笔|部门";
                frmshow.strwhere = txtbgry.Text.Trim();
                frmshow.chKht.Visible = false;
                frmshow.chKht.Checked = false;
                frmshow.ShowDialog();
                mSelectRow = frmshow.mSelectRow;
                if (mSelectRow != null && mSelectRow.Index >= 0)
                {
                    this.txtbgry.Tag = mSelectRow.Cells["代码"].Value.ToString();
                    this.txtbgry.Text = mSelectRow.Cells["名称"].Value.ToString();
                    this.btnAdd.Focus();
                }
                else
                {

                    return;
                }
            }
        }

        private void txtrkry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DataGridViewRow mSelectRow = new DataGridViewRow();
                DataTable dt = new DataTable();
                dt = Dal.stk.Sys_GetRYXX().Tables[0];
                Frm_Query frmshow = new Frm_Query(dt, "");
                frmshow.filter = "代码|名称|拼音|五笔|部门";
                frmshow.strwhere = txtrkry.Text.Trim();
                frmshow.chKht.Visible = false;
                frmshow.chKht.Checked = false;
                frmshow.ShowDialog();
                mSelectRow = frmshow.mSelectRow;
                if (mSelectRow != null && mSelectRow.Index >= 0)
                {
                    this.txtrkry.Tag = mSelectRow.Cells["代码"].Value.ToString();
                    this.txtrkry.Text = mSelectRow.Cells["名称"].Value.ToString();
                    this.txtysry.Focus();
                }
                else
                {

                    return;
                }
            }
        }

        private void txtysry_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DataGridViewRow mSelectRow = new DataGridViewRow();
                DataTable dt = new DataTable();
                dt = Dal.stk.Sys_GetRYXX().Tables[0];
                Frm_Query frmshow = new Frm_Query(dt, "");
                frmshow.filter = "代码|名称|拼音|五笔|部门";
                frmshow.strwhere = txtysry.Text.Trim();
                frmshow.chKht.Visible = false;
                frmshow.chKht.Checked = false;
                frmshow.ShowDialog();
                mSelectRow = frmshow.mSelectRow;
                if (mSelectRow != null && mSelectRow.Index >= 0)
                {
                    this.txtysry.Tag = mSelectRow.Cells["代码"].Value.ToString();
                    this.txtysry.Text = mSelectRow.Cells["名称"].Value.ToString();
                     this.txtbgry.Focus();
                }
                else
                {

                    return;
                }
            }
        }

        private void txtbgry_Leave(object sender, EventArgs e)
        {
            if (txtysry.Text.Trim().Length == 0)
            {
                txtysry.Tag = "";
            }
            if (txtbgry.Text.Trim().Length == 0)
            {
                txtbgry.Tag = "";
            }
            if (txtrkry.Text.Trim().Length == 0)
            {
                txtrkry.Tag = "";
            }
        }

      

        private void txtsl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                 this.txtbz .Focus();
            }
        }

        private void txtbz_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btnAdd.Focus();
            }
        }

        private void btnprint_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet dsrept = Dal.stk.STK_CP_RKD_PRINT(txtrkd.Text.Trim());


                //dsrept.Tables[0].TableName = "STK_RKD";
                //dsrept.Tables[1].TableName = "STK_RKDMX";
                string strpath = Application.StartupPath + "\\Report\\成品入库单.rpt";
                List<string> lparam = new List<string>();
                lparam.Add("rptname|" + pulic.UserIfo.rptname);
                lparam.Add("rptname1|入库单");
                PrintForm frmprint = new PrintForm(dsrept, strpath, lparam);
                frmprint.ShowDialog();
            }
            catch (Exception ex)
            {
                pulic.Cls_Popup.MsgErrBox(ex.ToString());
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="flag"> true 可以 false</param>
        void setbtn( bool flag )
        {
            btnAdd.Enabled = flag;
            btnDelete.Enabled = flag;
            button3.Enabled = flag;
        
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
      
    }
}
